<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_user.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_code_generator.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_bet_calculators.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_security {

    public static function login($username, $password) {
        $result = array();
        $result['user_login'] = 'failed';
        $user_access = new dao_user();
        $user = $user_access->get_by_login_info($username, $password);
        if (!empty($user)) {
            $result = $user;
            $result['user_login'] = 'logined';
        }
        return $result;
    }

    public static function update_last_login($user_id) {
        $user_access = new dao_user();
        $now = lib_date::get_now();
        $user_access->update_last_login($user_id, $now);
    }

    public static function get_user_by_username($username) {
        $user_access = new dao_user();
        return $user_access->get_by_username($username);
    }
    
    public static function get_user_by_id($user_id) {
        $user_access = new dao_user();
        $user_access->get_by_id($user_id);
    }
    
    public static function activate($email, $activate_code) {
        $user_access = new dao_user();
        $user = $user_access->get_by_activate_code($email, $activate_code);
        if (!empty($user)) {
            $user_access->active($user['id']);
            return TRUE;
        }
        return FALSE;
    }

    public static function logout() {
        session_start();
        $_SESSION['user_login'] = '';
        unset($_SESSION['user_info']);
        lib_redirect::Redirect('/login.php');
    }

    public static function register($username, $email, $password,
            $first_name, $last_name, $phone_number, $invite_by_user_id) {
        $user_access = new dao_user();
        $activate_code = lib_code_generator::generate_random_string();
        $inserted_id = $user_access->register_normal($first_name,$last_name,$phone_number, $email,$username,$password,$activate_code, $invite_by_user_id);
        api_bet_calculators::update_affiliate_profit($inserted_id, $invite_by_user_id);
        return $activate_code;
    }

    public static function validate_register($first_name, $last_name,
            $phone_number, $email, $username, $password, $password_confirm, $captcha) {
        $error_show = "";
        if (empty($first_name)) {
            $error_show .= "<li>Please fill First Name.</li>";
        }
        if (empty($last_name)) {
            $error_show .= "<li>Please fill Last Name.</li>";
        }
        if (empty($phone_number)) {
            $error_show .= "<li>Please fill Phone Number.</li>";
        }
        if (empty($email)) {
            $error_show .= "<li>Please fill Email.</li>";
        }
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error_show .= "<li>Email invalid.</li>";
        }
        if (self::email_exists($email)) {
            $error_show .= "<li>Email already exists.</li>";
        }
        if (empty($username)) {
            $error_show .= "<li>Please fill Username.</li>";
        }
        if (self::username_exists($username)) {
            $error_show .= "<li>Username already exists.</li>";
        }
        if (empty($password)) {
            $error_show .= "<li>Please fill Password.</li>";
        }
        if (empty($password_confirm) || $password_confirm !== $password) { //match password
            $error_show .= "<li>You have to confirm password exactly!</li>";
        }
        if ($_SESSION['cap_code'] !== $captcha) {
            $error_show .= "<li>Enter the captcha code wrong.</li>";
        }
        return $error_show;
    }

    public static function username_exists($username) {

        $dao_user = new dao_user();
        $list = $dao_user->get_by_username($username);
        if (!empty($list) && count($list) > 0) {

            return TRUE;
        }
        return FALSE;
    }

    public static function email_exists($email) {

        $dao_user = new dao_user();
        $list = $dao_user->get_by_email($email);
        if (!empty($list) && count($list) > 0) {

            return TRUE;
        }
        return FALSE;
    }

    public static function get_all_users() {
        $user_access = new dao_user();
        return $user_access->get_all();
    }

}
